package com.mfhcd.facepay.utils;

import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.databinding.BindingAdapter;

import com.bumptech.glide.Glide;
import com.mfhcd.facepay.App;

/**
 * @Author：张洪顺
 * @E-Mail: zhanghs@mfhcd.com
 * @Date：2019-09-24 16:15
 * @版权所有 © 现代金融控股（成都）有限公司
 * @描述：databinding 中的自定义属性 ImageView 与 Glide 结合使用
 */
public class ViewAttrAdapter {

    @BindingAdapter("imgResId")
    public static void setSrcByResId(ImageView imageView, int resId) {
        if (resId == 0) {
            return;
        }
        Glide.with(App.getInstance()).load(resId).into(imageView);
    }

    @BindingAdapter({"imageUrl"})
    public static void setImageUrl(ImageView imageView, String url) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        Glide.with(App.getInstance()).load(url).into(imageView);
    }

    @BindingAdapter({"imageUrlPlaceholder", "placeholder", "error"})
    public static void loadImage(ImageView imageView, String url, Drawable holderDrawable, Drawable errorDrawable) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        Glide.with(App.getInstance())
                .load(url)
                .placeholder(holderDrawable)
                .error(errorDrawable)
                .into(imageView);
    }

    @BindingAdapter("android:layout_marginTop")
    public static void setTopMargin(View view, int topMargin) {
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        layoutParams.setMargins(layoutParams.leftMargin, topMargin, layoutParams.rightMargin, layoutParams.bottomMargin);
        view.setLayoutParams(layoutParams);
    }
}
